package com.pl.lister;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pl.entity.Device;
import com.pl.entity.SysUser;
import com.pl.service.DeviceService;
import com.pl.service.SysUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author YanHaoLong
 * @date 2024/5/5
 * <p>
 * -- 代码敲烂 月薪过万--
 */
public class deviceReadLister extends AnalysisEventListener<Device> {
    //定义初始加入的记录数
    private static final int BATCH_COUNT=5;
    //引入SysUserService,后边导入会用到
    private DeviceService deviceService;
    //创建初始集合存sysUser数据
    List<Device> deviceList =new ArrayList<>();

    public deviceReadLister() {
    }
    public deviceReadLister(DeviceService deviceService) {
        this.deviceService = deviceService;
    }


    @Override
    public void invoke(Device device, AnalysisContext analysisContext) {
        deviceList.add(device);
        if (deviceList.size()>=BATCH_COUNT){
            saveDate();
            deviceList.clear();
        }

    }
    private void saveDate() {
        System.out.println("开始批量导入");
        deviceService.inserts(deviceList);
        System.out.println("结束批量导入");
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (deviceList.size()>0){  //最后一次存的地方
            saveDate();
            deviceList.clear();
        }
    }
}
